package com.paomi.client.fragment.order.presenter;

import android.app.Activity;
import android.app.Dialog;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.paomi.client.R;
import com.paomi.client.base.BaseLoadMorePresenter;
import com.paomi.client.base.BaseMvpLCEView;
import com.paomi.client.bean.BaseJSON;
import com.paomi.client.fragment.order.model.OrderListBean;
import com.paomi.client.net.Api;
import com.paomi.client.net.BaseCallback;
import com.paomi.client.net.Constants;
import com.paomi.client.view.LoadingDialog;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import retrofit2.Call;

/**
 * 已完成订单
 */
public class OrderCompletPre extends BaseLoadMorePresenter<BaseMvpLCEView> {
    private List<OrderListBean.OrderData> orderDataList=new ArrayList<>();
    private String state=null;//订单状态 0 未支付 1 已支付 2 待发货 3 待收货 4 待评价（已收货） 5 已完成（已评价） 6 退款申请中 7 已退款
    private int pageN;//页码数
    public void getListOrder(String state,int pageN){
        this.state=state;
        this.pageN=pageN;
        loadData(true);
    }
    @Override
    protected void loadData(int page, int skip, boolean pullToRefresh) {
        if(state!=null){
            Api.getInstanceService(Constants.Ticket_URL).myOrderList(state,""+pageN,""+Constants.PAGE_SIZE).enqueue(new BaseCallback<OrderListBean>() {
                @Override
                public void onResponse(Call<OrderListBean> call, OrderListBean result) {
                    if (pageN==1){
                        orderDataList.clear();
                    }
                    if(result.getReturnData().getData()!=null){
                        orderDataList.addAll(result.getReturnData().getData());
                    }
                    //处理价格相同的归类
                    for(int i=0;i<orderDataList.size();i++){
                        OrderListBean.OrderData orderData=orderDataList.get(i);
                        List<OrderListBean.OrderGoods> goodsList=orderData.getOrderGoodsTickets();
                        Map<String,List<OrderListBean.OrderGoods>> orderMap=sortAmountList(goodsList);
                        orderData.setOrderList(orderMap);
                        //如果需要再次对分组后的数据进行操作,可以取出对应的数据
                        Set<String> keySet = orderMap.keySet();
                        List<OrderListBean.OrderGoods> goodsList1=new ArrayList<>();
                        for(String groupName : keySet) {
                            OrderListBean.OrderGoods ticketJson1 = new OrderListBean.OrderGoods();
                            ticketJson1.setAmount(Float.parseFloat(groupName));
                            ticketJson1.setNum(orderMap.get(groupName).size());
                            String stringType="";
                            for(OrderListBean.OrderGoods seatBean:orderMap.get(groupName)){
                                stringType +=seatBean.getGoodsTicketName()+"、";
                            }
                            ticketJson1.setGoodsTicketName(stringType.substring(0,stringType.length()-1));
                            goodsList1.add(ticketJson1);
                        }
                        orderData.setOrderGoodsTickets(goodsList1);
                    }
                    getView().setData(orderDataList, true);
                }
            });
        }else{
            getView().setData(orderDataList, true);
        }
        if(orderDataList.size()==0){
            getView().setData(orderDataList, true);
        }
        LoadingDialog.get().hideLoading();
    }
    /**
     *用户-订单-确认收货
     * @param orderId
     */
    public void getOrderConfim(String orderId){
        Api.getInstanceService(Constants.Ticket_URL).orderConfirm(orderId).enqueue(new BaseCallback<BaseJSON>() {
            @Override
            public void onResponse(Call<BaseJSON> call, BaseJSON result) {
                result.returnMessage="确认收货";
                new Handler().postDelayed(new Runnable(){
                    public void run() {
                        EventBus.getDefault().post(result);
                    }
                }, 100);
            }
        });
    }
    /**
     * 订单删除的操作
     *
     * @param activity
     */
    public void deleteBtn(Activity activity, String id) {
        LayoutInflater inflater = LayoutInflater.from(activity);
        View view = inflater.inflate(R.layout.dialog_delete_shaow, null);
        final Dialog dialog = new Dialog(activity, R.style.theme_dialog);
        dialog.setContentView(view);
        dialog.show();
        TextView title = view.findViewById(R.id.title);
        title.setText(activity.getResources().getString(R.string.del_order));
        TextView msg_tv = view.findViewById(R.id.msg_tv);
        msg_tv.setText(activity.getResources().getString(R.string.delete_order));
        TextView textCancel = view.findViewById(R.id.btn_qd);
        textCancel.setText(activity.getResources().getString(R.string.no_btn));
        textCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        TextView textSure = view.findViewById(R.id.btn_success);
        textSure.setText(activity.getResources().getString(R.string.yes_btn));
        textSure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                getOrderDelete(id);
            }
        });
    }
    /**
     *用户-订单-删除
     * @param orderId
     */
    public void getOrderDelete(String orderId){
        Api.getInstanceService(Constants.Ticket_URL).orderDelete(orderId).enqueue(new BaseCallback<BaseJSON>() {
            @Override
            public void onResponse(Call<BaseJSON> call, BaseJSON result) {
                result.returnMessage="订单删除";
                new Handler().postDelayed(new Runnable(){
                    public void run() {
                        EventBus.getDefault().post(result);
                    }
                }, 100);
            }
        });
    }
    /**
     *用户-订单-取消
     * @param orderId
     */
    public void getOrderCancel(String orderId){
        Api.getInstanceService(Constants.Ticket_URL).orderCancel(orderId).enqueue(new BaseCallback<BaseJSON>() {
            @Override
            public void onResponse(Call<BaseJSON> call, BaseJSON result) {
                result.returnMessage="订单取消";
                new Handler().postDelayed(new Runnable(){
                    public void run() {
                        EventBus.getDefault().post(result);
                    }
                }, 100);
            }
        });
    }
    /**
     * 根据价格分类数据
     * @param list
     * @return
     */
    public Map<String,List<OrderListBean.OrderGoods>>  sortAmountList(List<OrderListBean.OrderGoods> list){
        TreeMap tm=new TreeMap();
        for(int i=0;i<list.size();i++){
            OrderListBean.OrderGoods s=list.get(i);
            if(tm.containsKey(""+s.getAmount())){//根据价格分类
                List<OrderListBean.OrderGoods> l11=(List<OrderListBean.OrderGoods>)tm.get(""+s.getAmount());
                l11.add(s);
            }else{
                List<OrderListBean.OrderGoods> tem=new ArrayList<>();
                tem.add(s);
                tm.put(""+s.getAmount(), tem);
            }
        }
        return tm;
    }
}